import {get, post, put, deleteRequest} from '/@/utils/http/axios';
import {AUTHORIZATION} from "/@/store/constants";

enum URL {
    // 角色接口
    queryList = '/api/system/role',
    create = '/api/system/role',
    update = '/api/system/role',
    // 启用状态的角色列表接口
    activeList = '/api/system/role/activeList',
    // 查询角色绑定了资源列表的接口
    bindingResourceDetails = '/api/system/role/resource',
    // 角色绑定资源接口
    bindingResource = '/api/system/role/bindingResource',
    // 查看角色关联用户列表接口
    queryRelUsers = '/api/system/role/relUsers',

    // 删除角色接口
    delete = '/api/system/role/delete'
}

const token =  localStorage.getItem(AUTHORIZATION);

export interface LoginData {
    username: string;
    password: string;
}

const queryRoleListApi = async (params: any) => get<any>({
    url: URL.queryList, params: params, data: {},
    headers: {
        'Authorization': `Bearer ${token}`
    }
});
const queryActiveRoleListApi = async (params: any) => get<any>({
    url: URL.activeList, params: params, data: {},
    headers: {
        'Authorization': `Bearer ${token}`
    }
});
const createApi = async (data: any) => post<any>({
    url: URL.create, params: {}, data: data,
    headers: {'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json'}
});
const updateApi = async (data: any) => put<any>({
    url: URL.update,
    data: data,
    headers: {'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json'}
});

const deleteApi = async (params: any) => deleteRequest<any>({
    url: URL.delete,
    params: params,
    headers: {'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json'}
});

const bindingResourceApi = async (data: any) => post<any>({
    url: URL.bindingResource, params: {}, data: data,
    headers: {'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json'}
});

const bindingResourceDetailsApi = async (id: number) => get<any>({
    url: URL.bindingResourceDetails +"/"+ `${id}`,
    params: {},
    data: {},
    headers: {'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json'}
});

const queryRelUsersApi = async (params: any) => get<any>({
    url: URL.queryRelUsers, params: params, data: {},
    headers: {
        'Authorization': `Bearer ${token}`
    }
});


export {
    queryRoleListApi,
    queryActiveRoleListApi,
    createApi,
    updateApi,
    deleteApi,
    bindingResourceApi,
    bindingResourceDetailsApi,
    queryRelUsersApi
};
